var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            pageindex: 0,
            pagesize: 10,
            status: 1, //0-全部 1-待派单 2-待接单 3-待处理 4-已完工 5-已查验
            isFirstLoad: 1,
            ProjectIDs: [],
            keywords: '',
        },
        total: {
            PublicCount: 0,//待派单数量
            AcceptCount: 0,//待接单数量
            HandleCount: 0,//处理中数量
            CompleteCount: 0,//已完工数量
            CheckCount: 0,//已查验数量
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showsearchmonth: false,
        show: false,
        showsearch: false,
        showTop: false,
        projectList: [],
    },
    methods: {
        doReset: function (type) {
            var that = this;
            if (type == 2) {
            } else {
                that.form.ProjectIDs = [];
            }
        },
        do_search: function () {
            var that = this;
            that.form.isFirstLoad = 1;
            that.doHideSearch();
            that.get_data();
        },
        doSelectProject: function (ID) {
            var that = this;
            var index = that.form.ProjectIDs.indexOf(ID);
            if (index > -1) {
                that.form.ProjectIDs.splice(index, 1);
            } else {
                that.form.ProjectIDs.push(ID);
            }
        },
        getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "getanalysissearchparam";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_open_search: function () {
            var that = this;
            that.show = !that.show;
        },
        get_data: function () {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.action = "getdeviceservicelist";
            options.ProjectIDs = JSON.stringify(that.form.ProjectIDs);
            options.Keywords = that.form.keywords;
            options.Status = that.form.status;
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                that.is_searching = false;
                if (succeed) {
                    if (data.data.List.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.data.List;
                    } else {
                        that.list = that.list.concat(data.data.List);
                    }
                    if (that.form.isFirstLoad == 1) {
                        that.total.PublicCount = data.data.Summary.PublicCount; //待派单
                        that.total.AcceptCount = data.data.Summary.AcceptCount; //待接单
                        that.total.HandleCount = data.data.Summary.HandleCount;
                        that.total.CompleteCount = data.data.Summary.CompleteCount;
                        that.total.CheckCount = data.data.Summary.CheckCount;
                    }
                    that.current_item_length = that.list.length;
                    that.form.isFirstLoad = 0;
                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        select_status: function (status) {
            var that = this;
            that.showTop = false;
            that.form.status = status;
            that.list = [];
            that.form.isFirstLoad = 1;
            that.get_data();
        },
        openGongdan: function (item) {
            var that = this;
            ns.openWin('repairdetail_frm', '工单详情', {
                id: item.ID,
                isviewdetail: that.form.isviewdetail
            });
        },
        //接单
        do_accpet: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "deviceserviceaccept";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        that.select_status(0);
                    } else {
                        api.toast({
                            msg: '状态已改变，请重新选择',
                            duration: 2000,
                            location: 'bottom'
                        });

                    }
                    api.sendEvent({
                        name: 'reloadrepairlist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        // 派单
        do_open_chooseuser: function (item, type) {
            var that = this;
            ns.openWin('repairchooseuser', '派单处理', {
                ID: item.ID,
                type: type,
                ServiceType: 1,
                IsMultiple:true,//是否是多选
            });
        },
        //处理
        do_chuli: function (item) {
            var that = this;
            ns.openWin('repairrecord_frm', '维修记录', {
                id: item.ID,
            });
        },
        //查验
        do_complete: function (item) {
            var that = this;
            ns.openWin('repaircomplete_frm', '完工查验', {
                id: item.ID,
            });
            // var options = {};
            // options.ID = item.ID;
            // options.action = "getcustomerservicestatus";
            // ns.post(options, function (succeed, data, err) {
            //     if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
            //         ns.openWin('repaircomplete_frm', '完工查验', {
            //             id: item.ID,
            //         });
            //     } else {
            //         api.toast({
            //             msg: '状态已改变，请重新选择',
            //             duration: 2000,
            //             location: 'bottom'
            //         });
            //     }
            //     api.sendEvent({
            //         name: 'reloadrepairlist',
            //     });
            // }, {
            //     toast: true,
            //     toastmsg: '提交中'
            // });
        },
        doHideSearch: function () {
            var that = this;
            that.showTop = false;
            that.showsearch = false;
            that.show = false;
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    dialog = new auiDialog();
    toast = new auiToast();
    app.form.isviewdetail = api.pageParam.isviewdetail || false;
    if (app.form.isviewdetail) {
        app.form.status = 4;
    }
    api.addEventListener({
        name: 'reloadrepairlist'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });

    ns.init_pull();
    ns.init_search();
    app.getSearchParams();
    app.get_data();
};
